<?php
    require("./Utils.php");
    $data = array();

    $post = getJsonParamter();
    $username = $post['username'];

    $page = $_GET['page'];
    $count = 10;
    $sql_page = ($page - 1) * $count;

    $conn = getConnection();
    $sql = "SELECT o.id, date, status, SUM(price) price
            FROM `order` o LEFT JOIN pet_order po 
            ON o.id = po.order_id
            LEFT JOIN pet p
            ON po.pet_id = p.id
            WHERE user_id = ? GROUP BY o.id
             limit ? , ?";

    $res = query_list($conn, $sql, $username, $sql_page, $count);
    $data['order_list'] = $res;

    $sql = "SELECT count(*) count_total FROM `order`";
    $res = query($conn, $sql);
    
    $total_page = $res['count_total'] % $count > 0 ? $res['count_total'] / $count + 1 : $res['count_total'] / $count;

    $data['nowPage'] = $page;
    $data['totalPage'] = (int)$total_page;

    getJson(200, '获取成功', $data);
?>